package com.puboot.module.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.puboot.common.constants.BaseConstants;
import com.puboot.common.util.CoreConst;
import com.puboot.common.util.Pagination;
import com.puboot.module.blog.mapper.AgreementMapper;
import com.puboot.module.blog.mapper.CustomerMapper;
import com.puboot.module.blog.model.Agreement;
import com.puboot.module.blog.model.Company;
import com.puboot.module.blog.model.Customer;
import com.puboot.module.blog.service.CompanyService;
import com.puboot.module.blog.service.CustomerService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static org.apache.shiro.crypto.hash.Sha256Hash.ALGORITHM_NAME;

@Service
@AllArgsConstructor
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    private final CompanyService companyService;

    private CustomerMapper customerMapper;

    private AgreementMapper agreementMapper;

    public Customer getByUsername(String username) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional
    public void regist(String username, String nicname, String password, String companyname, String deptname, String industry, String province, String city) {
        //加密密码
        String newPassword = new SimpleHash(ALGORITHM_NAME, password, ByteSource.Util.bytes(username), 2).toHex();

        //生成用户信息存入数据库 注册成功
        Customer customer = new Customer();
        customer.setUsername(username);
        customer.setPassword(newPassword);
        customer.setCreateTime(new Date());
        customer.setUpdateTime(new Date());
        customer.setState(BaseConstants.VAILD);

        customer.setNicname(nicname);
        customer.setBalance(0);

        if (StringUtils.isNotBlank(companyname) && StringUtils.isNotBlank(deptname) && StringUtils.isNotBlank(industry) && StringUtils.isNotBlank(province) && StringUtils.isNotBlank(city)) {
            customer.setType(BaseConstants.BUSINESS_CUSTOMER);

            Company company = new Company();

            company.setName(companyname);
            company.setDept(deptname);
            company.setIndustry(industry);
            company.setProvince(province);
            company.setCity(city);
            company.setState(CoreConst.STATUS_VALID);
            company.setCreateTime(new Date());
            company.setUpdateTime(new Date());
            companyService.save(company);
            customer.setCompanyId(company.getId());
        } else {
            customer.setType(BaseConstants.PERSON_CUSTOMER);
        }
        baseMapper.insert(customer);
    }

    @Override
    public IPage<Customer> pageCustomerAll(Customer customer, Integer pageNumber, Integer pageSize) {
        IPage<Customer> page = new Pagination<>(pageNumber, pageSize);
        return page.setRecords(customerMapper.findAll(page, customer));
    }

    @Override
    public Integer updateStateByIds(Integer state, List<Integer> ids){
        return customerMapper.updateStateByIds(state, ids);
    }

    @Override
    public void changepassword(String username, String password) {
        //加密密码
        String newPassword = new SimpleHash(ALGORITHM_NAME, password, ByteSource.Util.bytes(username), 2).toHex();
        baseMapper.updatePasswordByUsername(newPassword, username);
    }

    @Override
    public Agreement getAgreement() {
       return agreementMapper.selectById(1);
    }
}
